package nccloud.web.hrpbm.sjxm.sjxmh.action;

import nc.bs.framework.common.InvocationInfoProxy;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.vo.pf.flowinstance.NCCFlowInstanceVO;
import nccloud.vo.riawf.pub.pf.FlowEditFieldVO;
import nccloud.web.codeplatform.framework.action.base.BaseAction;
import nccloud.web.codeplatform.framework.action.base.RequestDTO;
import nccloud.web.codeplatform.framework.action.base.RequstParamWapper;
import nccloud.web.codeplatform.framework.action.base.VOTransform;
import nc.itf.pub.workflowqry.IFlowInstanceQuery;
import nc.itf.uap.pf.IWorkflowService;

import java.util.List;
import java.util.Map;

/**
*  流程字段权限可以实现单据字段在不同环节时具有不同的必填、可编辑、可见属性。
*  具体见审批流定义-流程字段权限配置
*/
public class PFlowEditableSjxmHVOAction  extends BaseAction {
    @Override
    public Object doAction(IRequest request, RequstParamWapper paramWapper) throws Throwable {
        RequestDTO param = VOTransform.toVO(paramWapper.requestString,RequestDTO.class);

        Map<String,Object> userJson = param.getUserJson();
        if(userJson==null|| StringUtils.isEmpty((String)userJson.get("billtype"))){
            ExceptionUtils.wrapBusinessException("单据类型或交易类型编码不能为空！");
        }
        //单据类型或交易类型
        String transOrBilltype = (String)userJson.get("billtype");

        NCCFlowInstanceVO[] instanceVO = ServiceLocator.find(IFlowInstanceQuery.class).findFlowInstancesByBillIdAndBillType(param.getPk(),transOrBilltype);

        if(instanceVO==null||instanceVO.length==0){//未找到流程实例
            return null;
        }
        int type = Integer.parseInt(instanceVO[0].getWorkflow_type());
        List<FlowEditFieldVO> fields = ServiceLocator.find(IWorkflowService.class).getFLowPreopertiesForTemplate(
                InvocationInfoProxy.getInstance().getUserId(),// 审批人，取当前用户主键即可
                transOrBilltype,//交易类型或单据类型编码
                param.getPk(), //单据主键
                type);//流程类型WorkflowTypeEnum.Approveflow或WorkflowTypeEnum.Workflow

        return fields;
    }
}
